package com.kaibes.core.base.option;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import com.kaibes.core.base.function.ToBooleanFunction;
import com.kaibes.core.base.function.ToIntegerFunction;
import com.kaibes.core.base.function.ToLongFunction;
import com.kaibes.core.base.function.ToStringFunction;

public class NullOptions {

    public static <T> void ifNotNull(T obj, Consumer<T> consumer) {
        if (obj != null) {
            consumer.accept(obj);
        }
    }
    
    public static <E, R> R checkNull(E obj, Function<E, R> function, Supplier<R> supplier) {
        return obj==null?supplier.get():function.apply(obj);
    }
    
    //==========
    public static <T> Integer returnInteger(T obj, ToIntegerFunction<T> function) {
        return obj==null?null:function.applyAsInteger(obj);
    }
    public static <T> Long returnLong(T obj, ToLongFunction<T> function) {
        return obj==null?null:function.applyAsLong(obj);
    }
    public static <T> String returnString(T obj, ToStringFunction<T> function) {
        return obj==null?null:function.applyAsString(obj);
    }
    
    public static <T> boolean returnFalse(T obj, ToBooleanFunction<T> function) {
        return obj==null?false:function.applyAsBoolean(obj);
    }
    public static <T> boolean returnTrue(T obj, ToBooleanFunction<T> function) {
        return obj==null?true:function.applyAsBoolean(obj);
    }
    
}
